<?php
/*
Anhnt317@gmail.com
*/

    class YouTube{      
        //Get the ID of a video from any youtube link
        function getID( $url ){
            if( !strpos($url, "#!v=") === false ){  
                //In case of be a link from related
                $url = str_replace('#!v=','?v=',$url);
            }
            parse_str( parse_url( $url, PHP_URL_QUERY ) );
            if( isset( $v ) ){
                return $v;
            } else { 
                //If not found, is because is a link from a user channel ex: http://www.youtube.com/user/laryssap#p/a/u/1/SAXVMaLL94g
                return substr( $url, strrpos( $url,'/') + 1, 11);
            }
        }       
        //Generate a youtube link from a video ID
        function generateLink( $id ){
            return 'http://www.youtube.com/watch?v=' . trim($id);
        }        
        //Generate a simple embedded code from a video ID
        //Gera um code de embedded simples, a partir de um ID de video
        function generateEmbedded( $id, $width = 320, $height = 240 ){        
            return  '<object width="' . $width . '" height="' . $height . '">
                        <param name="movie" value="http://www.youtube.com/v/' . $id . '?fs=1&amp;hl=pt_BR&amp;rel=0"></param>
                        <param name="allowFullScreen" value="true"></param>
                        <param name="allowscriptaccess" value="always"></param>
                        <embed src="http://www.youtube.com/v/' . $id . '?fs=1&amp;hl=pt_BR&amp;rel=0"
                            type="application/x-shockwave-flash"
                            allowscriptaccess="always"
                            allowfullscreen="true"
                            width="' . $width . '"
                            height="' . $height . '">
                        </embed>
                    </object>';
        }
    }    
?>